import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'

import Home from '../views/Home.vue'
//
import Taocan from '../views/homelist/Taocan.vue'
import Customized from '../views/homelist/Customized.vue'
import Health from '../views/homelist/Health.vue'
import Dongyingong from '../views/homelist/Dongyingong.vue'
import Salad from '../views/homelist/Salad.vue'
import Snack from '../views/homelist/Snack.vue'
import Culiang from '../views/homelist/Culiang.vue'
import Juice from '../views/homelist/Juice.vue'

import Cart from '../views/Cart.vue'

import Login from '../views/Login.vue'
import Menu from '../views/Menu.vue'
import Reg from '../views/Reg.vue'


import Breakage from '../views/manage/breakage/Breakage.vue'
import Check from '../views/manage/check/Check.vue'
import EditGoods from '../views/manage/editgoods/EditGoods.vue'
import SoldNote from '../views/manage/history/SoldNote.vue'
import Inquire from '../views/manage/inventory/Inquire.vue'
import NewGoods from '../views/manage/newgoods/NewGoods.vue'
import OnlineOrders from '../views/manage/onlineorders/OnlineOrders.vue'
import LabelPrinting from '../views/manage/print/LabelPrinting.vue'
import SalesReturn from '../views/manage/salesreturn/SalesReturn.vue'
import System from '../views/manage/system/System.vue'
import TheCargo from '../views/manage/thecargo/TheCargo.vue'
import NewVip from '../views/manage/vip/NewVip.vue'

//pinia
import {useUserStore} from '@/store/user'



const router = createRouter({
    history: process.env.NODE_ENV === 'production' ? createWebHistory() : createWebHashHistory(),

    routes:[
        {path:'/',redirect:'menu'},
        {path:'/home',component:Home,name:'Home',
        },
        { path: '/login', component: Login, name: 'Login' ,
        beforeEnter(to,from,next){
            // 如果用户已经登录，则跳到/home
            const userStore = useUserStore()
                if(userStore.isLogin){
                    next('/menu')
                }else{
                    next()
                }
        }},
        { path: '/reg', component: Reg, name: 'Reg' },
        { path: '/menu', component: Menu, name: 'Menu', },
        
        { path: '/breakage', component: Breakage },
        { path: '/check', component: Check },
        { path: '/editgoods', component: EditGoods },
        { path: '/soldnote', component: SoldNote },
        { path: '/inquire', component: Inquire },
        { path: '/newgoods', component: NewGoods },
        { path: '/onlineorders', component: OnlineOrders },
        { path: '/labelprinting', component: LabelPrinting },
        { path: '/salesreturn', component: SalesReturn },
        { path: '/system', component: System },
        { path: '/thecargo', component: TheCargo },
        { path: '/vip', component: NewVip },


      

    ]
})
// 判断用户是否登录
// 如已登录，则添加/menu路由
router.beforeEach((to,from,next)=>{
    if(to.path.startsWith('/menu')){
        const store = useUserStore()
        if(store.isLogin){
            next();
        }else{
            router.push('/login')
        }
    }else{
        next()
    }  
})

// console.log(store);


export default router